#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve() {
    int n; cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++)cin >> a[i];
    int t = *min_element(a.begin(), a.end());
    int pos = -1;
    for(int i = 0; i < n; i++) {
        if(a[i] == t) {
            pos = i;
            break;
        }
    }
    for(int i = pos + 1; i < n; i++) {
        if(a[i] < a[i - 1]) {
            cout << -1 << endl;
            return;
        }
    }
    cout << pos << endl;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--) solve();
    return 0;
}